package com.hnevc.netfoodchoose;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class ShowFoodActivity extends AppCompatActivity {

    List<Food> mFoodList; //食物列表
    ListView mListView;
    MyBaseAdapter mAdapter; //适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_food);
        initView();
        initData();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.id_lv_food_list);
    }

    /**
     * 初始化数据，用来获取食物列表的数据
     */
    private void initData() {
        //TODO 访问网络获取数据，填充mFoodList,然后创建数据适配器，更新界面
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url("http://10.1.63.232:8080/RestaurantServer/Food/getFoods")
                        .get()
                        .build();

                OkHttpClient client = new OkHttpClient.Builder()
                        .cookieJar(new MyCookieJar())
                        .build();
                try{
                    Response response = client.newCall(request).execute();
                    String data = response.body().string();
                    Gson gson = new Gson();
                    mFoodList = gson.fromJson(data,new TypeToken<List<Food>>(){}.getType());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //TODO  更新界面
                            MyBaseAdapter adapter = new MyBaseAdapter();
                            mListView.setAdapter(adapter);
                        }
                    });



                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //TODO 根据数据mFoodList，自定义数据适配器
    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mFoodList.size();
        }

        @Override
        public Object getItem(int i) {
            return mFoodList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return mFoodList.get(i).getFoodId();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(
                    ShowFoodActivity.this,
                    R.layout.food_list_item,
                    null
            );
            final Food food = mFoodList.get(i);
            TextView tvFoodName = (TextView) view.findViewById(R.id.id_tv_foodName);
            tvFoodName.setText(food.getFoodName());

            ImageView ivPic = (ImageView) view.findViewById(R.id.id_iv_foodPic);
            String imageurl = "http://10.1.63.232:8080/RestaurantServer/uploadFoodImage/"
                    +food.getFoodPic();
            Picasso.with(ShowFoodActivity.this)
                    .load(imageurl)
                    .into(ivPic);
            //设置。。。。点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO 跳转到FoodDetailActivity
                    Intent intent = new Intent();
                    intent.setClass(ShowFoodActivity.this,FoodDetailActivity.class);
                    //传递参数
                    intent.putExtra("foodId",food.getFoodId());

                    startActivity(intent);
                }
            });
            return view;
        }
    }
}
